

//删除购物车商品
function DelCar(cid) {

    $.ajax({
        url: "/IAjax/Ajax.ashx",
        type: "POST",
        cache: false,
        dataType: "text",
        data: { action: "DelCar", cid: cid },
        success: function (ReturnData) {
            if (ReturnData == "yes") {
                $("#car_" + cid).remove();


                $(".CarNum").html($(".cart-con .list li").length);

                var mytotal = 0;
                $(".cart-con .list li").each(function () {
                    var my = $(this).attr("total");
                    mytotal = accAdd(mytotal, parseFloat(my));
                });
                $(".total_price").html(mytotal);

            }
            else {
                alert(ReturnData);
            }
        }
    });
}
//清空购物车
function DelAllCar() {
    if (!confirm("Confirm empty shopping cart?")) {
        return;
    }
    $.ajax({
        url: "/IAjax/Ajax.ashx",
        type: "POST",
        cache: false,
        dataType: "text",
        data: { action: "DelAllCar" },
        success: function (ReturnData) {
            if (ReturnData == "yes") {
                alert("Empty the shopping cart successfully");
                window.location.href = "/";
            }
            else {
                alert(ReturnData);
            }
        }
    });
}

//增加减少购物车商品数量
function OperationNum(cid, model) {
    var buycount = $("#buy_num_" + cid).val();
    $.ajax({
        url: "/IAjax/Ajax.ashx",
        type: "POST",
        cache: false,
        dataType: "xml",
        data: { action: "AddOrReduceNum", cid: cid, model: escape(model), buycount: buycount },
        success: function (ReturnData) {
            $("#buy_num_" + cid).val($("response num", ReturnData).text());
            var price1 = ($("response price", ReturnData).text());
            var originalPrice1 = $("#originalPrice1_" + cid).text();

            $("#price_" + cid).text(price1);

            if (parseFloat(price1) == parseFloat(originalPrice1)) {
                $("#originalPrice1_" + cid).parent().hide();
            } else {
                $("#originalPrice1_" + cid).parent().show();
            }

            var state = $("response state", ReturnData).text();
            if (state != "yes") {
                alert($("response errorinfo", ReturnData).text());
            }
            else {
                Statistical();
            }
        }
    });
}
//统计数量和金额
//单价 class="single"
//数量 class="buynum"
//小计 class="total"
function Statistical() {


    $(".single").each(function (i) {
        $(".total").eq(i).html(multiple(parseFloat($(this).html()), parseFloat($(".buynum").eq(i).val())).toFixed(2));
    });



    //总金额与选择总数
    var mytotal = 0;
    var myselectnum = 0;
    var myallnum = 0;
    var original_price = 0;

    $(".mycheck input:checked").each(function () {
        var my = $(this).parents(".item").find(".total").html();
        mytotal = accAdd(mytotal, parseFloat(my)).toFixed(2);

        var mysel = $(this).parents(".item").find(".buynum").val();
        myselectnum = myselectnum + parseInt(mysel);
        myallnum++;

        var myoriginal = $(this).parents(".item").find(".originalsingle").html();
        original_price = accAdd(original_price, parseFloat(myoriginal * parseInt(mysel))).toFixed(2);
    });

    $("#alloriginal").html(original_price);
    $("#allprice").html(mytotal);

    var Discount = parseFloat(original_price) - parseFloat(mytotal)
    if (Discount > 0) {
        $("#alloriginal").parent().show();
    } else {
        $("#alloriginal").parent().hide();
    }

    $("#myallnum").html(myallnum);
    $("#selectnum").html(myselectnum);
    $("#Discount").html(Discount.toFixed(2));



}
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return ((arg1 * m + arg2 * m) / m)
}

function multiple(value1, value2) {
    var m = 0, s1 = value1.toString(), s2 = value2.toString();
    try {
        m += s1.split(".")[1].length;
    } catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    } catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
//地址
function ddladdress(a) {
    var aid = $(a).val();
    if (aid != null && aid != "") {
        $.ajax({
            url: "/IAjax/Ajax.ashx",
            type: "POST",
            cache: false,
            dataType: "xml",
            data: { action: "GetAddress", aid: aid },
            success: function (ReturnData) {
                var state = $("response state", ReturnData).text();
                if (state == "yes") {
                    $("#address_info").html($("response item", ReturnData).text());
                    $("#province_id").val($("response provinceid", ReturnData).text());

                }
                else {
                    alert($("response errorinfo", ReturnData).text());
                }
            }
        });
    }

}



//添加到收藏夹
function AddFavoriteUser(obj, pid) {

    $.ajax({
        url: "/IAjax/Ajax.ashx",
        type: "POST",
        cache: false,
        dataType: "text",
        data: { action: "AddFavoriteUser", pid: pid, purl: escape($("#purl").val()) },
        success: function (ReturnData) {
            if (ReturnData == "yes") {
                alert("Add to favorites successfully");
                $(obj).toggleClass("cur");

            }
            else if (ReturnData == "already") {
                alert("Unfavorite successfully");
                $(obj).removeClass("cur");
            }
            else {
                if (ReturnData == "no") {
                    alert("Please log in to your account first")
                    $(".login").fadeIn();
                }
                else {
                    alert(ReturnData)
                }
            }
        }
    });
}


